clear
set more off

local ylist 0 10 25 50 100 250 1000
local wc = wordcount("`ylist'")



cap file close myfile
file open myfile using "$outdir/tables/distribution.tex", write replace

file write myfile "	&	\multicolumn{1}{c}{Owners} "
file write myfile "	&	\multicolumn{3}{c}{Employees} "
file write myfile "	\\ "
file write myfile "	\cline{3-5} "

file write myfile " && Evenly-weighted & Wage-weighted & Capped wage-weighted \\ "
file write myfile " & (1) & (2) & (3) & (4) \\ "
file write myfile "\midrule" _n


	

	



forval i = 0/`=`wc'' {
	
	// write the header
	if `i' == 0 {
		file write myfile " \$ AGI < \\$0 \$ "
		local y0 neg
	}
	else if `i' == `wc' {
		file write myfile " \$ AGI \geq \\$1,000,000 \$ "
		local y0 1000
	}
	
	else if `i' >= 1 {
		local y0 : word `i' of `ylist'
		local y1 : word `=`i'+1' of `ylist'
		
		local y0_format : di %10.0fc `=`y0'*1000'
		local y1_format : di %10.0fc `=`y1'*1000'
		
		file write myfile " \$ \\$`y0_format' \leq AGI < \\$`y1_format' \$ "
	}
	
	// write the rows
	foreach yvar in owner_incm empl_even empl_wg empl_capped {
		use "$statadir/distributions_`yvar'", clear

		local ertc : di %6.3f d`y0'[1]
		
		file write myfile " & `ertc' "

	}
	file write myfile " \\ "
	
	if `i' < `wc' {
		file write myfile " \addlinespace[6pt] "
	}
		

}

cap file close myfile




